
#include "kheap.h"
#include "heap.h"
#include "config.h"
#include "textmode/textmode.h"
#include "memory/memory.h"

struct heap kernel_heap; 
struct heap_table kernel_heap_table; 

void kheap_init()
{
    const int total_table_entries = TESTOS_HEAP_SIZE_BYTES / TESTOS_HEAP_BLOCK_SIZE; 
    kernel_heap_table.entries = (HEAP_BLOCK_TABLE_ENTRY*)TESTOS_HEAP_TABLE_ADDRESS;
    kernel_heap_table.entries_amount = total_table_entries; 

    void* start = (void*)TESTOS_HEAP_ADDRESS;
    void* end = (void*)(TESTOS_HEAP_ADDRESS + TESTOS_HEAP_SIZE_BYTES); 
    const int res = heap_create(&kernel_heap, start, end, &kernel_heap_table); 
    if (res < 0) {
        print("Failed to create heap"); 
    }
}

void* kmalloc(const size_t size) 
{
    return heap_malloc(&kernel_heap, size); 
}

void* kzalloc(const size_t size)
{
    void* ptr = kmalloc(size); 
    if (!ptr) {
        return 0; 
    }

    memset(ptr, 0x00, size); 
    return ptr; 
}

void kfree(void* ptr)
{
    return heap_free(&kernel_heap, ptr); 
}